package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;


public class DeviceServlet extends HttpServlet {


	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3367156859166776527L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("***************&&&&&&&&&&&run");
		
		
		String filename = "Device1.jar";
		
		BufferedInputStream buf=null;
		ServletOutputStream myOut=null;
		
		
		String filepath="http://localhost:8081/DBP/DeviceDemo/";

		try{

		myOut = response.getOutputStream( );
		     File myfile = new File(filepath+filename);
		     
		     //set response headers
		     response.setContentType("application/jar");
		     
		     response.addHeader(
		        "Content-Disposition","attachment; filename="+filename );

		     response.setContentLength( (int) myfile.length( ) );
		     
		     FileInputStream input = new FileInputStream(myfile);
		     buf = new BufferedInputStream(input);
		     int readBytes = 0;

		     //read from the file; write to the ServletOutputStream
		     while(true)
		       myOut.write(readBytes);

		} catch (IOException ioe){
		     
		        throw new ServletException(ioe.getMessage( ));
		         
		     } finally {
		         
		     //close the input/output streams
		         if (myOut != null)
		             myOut.close( );
		          if (buf != null)buf.close( );
		         
		     }

		    
	}
}